<?php
/**
 * Mail functions
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andreas Gohr <andi@splitbrain.org>
 */

if(!defined('DOKU_INC')) die('meh.');
require_once(DOKU_INC.'inc/mail.php');


/**
 * UTF-8 autoencoding replacement for PHPs mail function
 *
 * Email address fields (To, From, Cc, Bcc can contain a textpart and an address
 * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded
 * automatically. You can seperate receivers by commas.
 *
 * @param string $to      Receiver of the mail (multiple seperated by commas)
 * @param string $subject Mailsubject
 * @param string $body    Messagebody
 * @param string $from    Sender address
 * @param string $cc      CarbonCopy receiver (multiple seperated by commas)
 * @param string $bcc     BlindCarbonCopy receiver (multiple seperated by commas)
 * @param string $headers Additional Headers (seperated by MAILHEADER_EOL
 * @param string $params  Additonal Sendmail params (passed to mail())
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 * @see    mail()
 */
function mail_send_html($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){

  $message = compact('to','subject','body','from','cc','bcc','headers','params');
  return trigger_event('MAIL_MESSAGE_SEND',$message,'_mail_send_action_html');
}

function _mail_send_action_html($data) {

  // retrieve parameters from event data, $to, $subject, $body, $from, $cc, $bcc, $headers, $params
  $to = $data['to'];
  $subject = $data['subject'];
  $body = $data['body'];

  // add robustness in case plugin removes any of these optional values
  $from = isset($data['from']) ? $data['from'] : '';
  $cc = isset($data['cc']) ? $data['cc'] : '';
  $bcc = isset($data['bcc']) ? $data['bcc'] : '';
  $headers = isset($data['headers']) ? $data['headers'] : null;
  $params = isset($data['params']) ? $data['params'] : null;

  // end additional code to support event ... original mail_send() code from here

  if(defined('MAILHEADER_ASCIIONLY')){
    $subject = utf8_deaccent($subject);
    $subject = utf8_strip($subject);
  }

  if(!utf8_isASCII($subject)) {
    $subject = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject,0).'?=';
    // Spaces must be encoded according to rfc2047. Use the "_" shorthand
    $subject = preg_replace('/ /', '_', $subject);
  }

  $header  = '';

  // No named recipients for To: in Windows (see FS#652)
  $usenames = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true;

  $to = mail_encode_address($to,'',$usenames);
  $header .= mail_encode_address($from,'From');
  $header .= mail_encode_address($cc,'Cc');
  $header .= mail_encode_address($bcc,'Bcc');
  $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL;
  $header .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL;
  $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL;
  $header .= $headers;
  $header  = trim($header);

  $body = mail_quotedprintable_encode($body);

  if($params == null){
    return @mail($to,$subject,$body,$header);
  }else{
    return @mail($to,$subject,$body,$header,$params);
  }
}

//Setup VIM: ex: et ts=2 enc=utf-8 :
